

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode head1 = new ListNode();
        head1.next = head;
        ListNode cur = head;
        ListNode pre = head1;
        if (head == null || head.next == null) {
            return head;
        }
        while (cur != null) {
            ListNode node = cur.next;
            if (node == null) {
                return head1.next;
            }
            cur.next = node.next;
            node.next = cur;
            pre.next = node;
            pre = cur;
            cur = cur.next;
        }
        return head1.next;
    }
}
